<template>
  <div class="app-container">
    <el-form label-width="68px" size="small" :inline="true">
      <el-form-item label="菜单">
        <el-select
          v-model="queryParams.menuId"
          placeholder="菜单类型"
          style="width: 100%"
        >
          <el-option
            v-for="dict in menuList"
            :key="dict.menuId"
            :label="dict.menuName"
            :value="dict.menuId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input
          placeholder="请输入标题"
          v-model="queryParams.title"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="small" @click="addHandle"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="postList">
      <el-table-column label="菜单名" align="center">
        <template slot-scope="scope">
          {{ scope.row.menuId | getTypeName(menuList) }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="更新时间" align="center" prop="pushTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toView(scope.row)"
            >修改</el-button
          >

          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form
        label-width="100px"
        size="small"
        :model="form"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="菜单类型" prop="menuId">
          <el-select
            v-model="form.menuId"
            placeholder="菜单类型"
            style="width: 100%"
          >
            <el-option
              v-for="dict in menuList"
              :key="dict.menuId"
              :label="dict.menuName"
              :value="dict.menuId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input
            placeholder="请输入标题"
            v-model="form.title"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input
            placeholder="请输入作者"
            v-model="form.author"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="remark">
          <imageUpload
            :limit="1"
            ref="imageUpload"
            v-model="form.remark"
            :uploadUrl="uploadUrl"
            @getAvatar="getAvatar"
          ></imageUpload>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getWordMenuList } from "@/api/menuAPP/menu";
import {
  addManager,
  getManagerList,
  eleManager,
  getManager,
  updateManager,
} from "@/api/menuAPP/text";
export default {
  data() {
    return {
      title: "",
      dialogFormVisible: false,
      uploadUrl:
        process.env.VUE_APP_BASE_API + "/system/user/profile/avatar/local",
      postList: [],
      menuList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: "",
        menuId: "",
      },
      total: 0,
      form: {},
      rules: {
        menuId: [
          { required: true, message: "请选择菜单类型", trigger: "blur" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        author: [{ required: true, message: "作者不能为空", trigger: "blur" }],
        remark: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
      },
    };
  },
  filters: {
    getTypeName(value, list) {
      const item = list.find((item) => item.menuId == value);
      return item ? item.menuName : "";
    },
  },
  methods: {
    reset() {
      this.form = {
        menuId: "",
        title: "",
        author: "",
        remark: "",
        content: " ",
      };
      this.resetForm("form");
    },
    addHandle() {
      this.dialogFormVisible = true;
      this.title = "新增";
      this.reset();
    },
    getAvatar(avatar) {
      this.form.remark = avatar;
    },
    getList() {
      getManagerList(this.queryParams).then((res) => {
        this.postList = res.rows;
        this.total = res.total;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.getList();
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.title === "新增") {
            addManager(this.form).then((res) => {
              this.$message.success("新增成功");
              this.dialogFormVisible = false;
              this.getList();
            });
          } else {
            updateManager(this.form).then((res) => {
              this.$message.success("修改成功");
              this.dialogFormVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    toView(row) {
      this.reset();
      this.title = "修改";
      this.dialogFormVisible = true;
      getManager(row.wordId).then((res) => {
        this.form = res.data;
      });
    },
    handleDelete(row) {
      this.$confirm("是否删除该数据", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          eleManager(row.wordId).then((res) => {
            this.$message.success("删除成功");
            this.getList();
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    getWordMenuList().then((res) => {
      this.menuList = res.rows;
    });
    this.getList();
  },
};
</script>
<style scoped>
::v-deep .el-form-item--small .el-form-item__label {
  text-align: justify;
}
</style>
